<?php
class AndroidController extends CController
{
    public function actionIndex()
    {
		$Account = Account::model()->find("login = :login AND password = :password",
							array(
								//":login"=>"te",
								//":password"=>"123")
								":login"=>Yii::app()->params->login,
								":password"=>Yii::app()->params->password)
							);

		if(isset($Account)) 
		{

		    $Contract = Contracts::model()->with("paidService.billType")->findByPk($Account->id_contract);
             $icon = Data::model()->find(array(
                                        'condition'=>'essense_id = :essense_id
                                                    AND c2 = :number_shop',
                                        'params'=>array(
                                                ':essense_id'=>911,
                                                ':number_shop'=>$Contract->number_shop,

                                            )
                                        ));

            $androidData = array(
				'real_url'=>isset($Contract->url) ? $Contract->url : 'shop.by',
                'img_url'=>'http://shop.by/pics/shops/'.(isset($icon->c4) ?$icon->c4 : 'default.jpg') ,
                'tp'=>array('name'=>$Contract->tariffPlan->name),
				'host'=>array('purse_sum'=>'0',
						'status'=>'noactive'),
					'db'=>array('purse_sum'=>'0',
						'status'=>'noactive'),
					'advance'=>array('purse_sum'=>'0',
						'status'=>'noactive'),
					);

			$paidServices = $Contract->paidService;

			foreach($paidServices as $paidService)
			{
				switch($paidService->id_bills_type)
				{
					case 2:
							$androidData['host'] = array(
                                'purse_sum'=> $this->getPurseSum($Account,$paidService->id_bills_type),
							    'status'=>$paidService->getServiceStatus(),
							);

						break;
					case 3:
						$androidData['db'] = array(
                            'purse_sum'=> $this->getPurseSum($Account,$paidService->id_bills_type),
							'status'=>$paidService->getServiceStatus(),
							);
						break;
					case 4:

						$androidData['advance'] = array(
                            'purse_sum'=> $this->getPurseSum($Account,$paidService->id_bills_type),
							'status'=>$paidService->getServiceStatus(),
							);
						break;
				}
			}


			echo CJSON::encode($androidData);
		}
    }

   private function getPurseSum($account, $id_bills_type)
   {
	    $purse = Purse::model()->find('id_purse_type=:id_purse_type
										   AND id_contract = :id_contract',
							array(':id_purse_type' => $id_bills_type - 1,
									':id_contract' => $account->id_contract));

   		$sql = "SELECT
   		        SUM(real_sum) as total_real_sum,
                SUM(full_sum) as total_full_sum
   		    FROM Bills
   		        WHERE status = :status
   		        AND id_purse = :id_purse
   		        AND id_contract = :id_contract
   		        AND action in (0,1)";

        $bills = Bills::model()->findBySql($sql,
                                          array(':status' => 1,
                                               'id_purse' => $purse->id_purse,
                                               ':id_contract' => $account->id_contract
                                          ));
		return ($purse->sum - $bills->total_full_sum - $bills->total_real_sum);
   }


    public function actionBalancelog()
    {
        $Account = Account::model()->find("login = :login AND password = :password",
			array(
					":login"=>Yii::app()->params->login,
					":password"=>Yii::app()->params->password)
				);

        if(isset($Account))
        {
            $purse = Purse::model()->find('id_purse_type=:id_purse_type
										   AND id_contract = :id_contract',
							        array(':id_purse_type' => 1,
									':id_contract' => $Account->id_contract));


            $bills = Bills::model()->findAll(
                array('select'=>'real_sum, real_date',
                    'condition'=>'operation_type = :operation_type AND id_purse = :id_purse',
                    'order'=>'real_date DESC',
                    'params'=> array(':id_purse' => $purse->id_purse,
									':operation_type' =>1)
            ));

            foreach($bills as $bill)
                if((int)$bill['real_sum'] > 0)
                    $array[] = array('sum'=>$bill['real_sum'],'date'=>date('d.m.Y H:m',strtotime($bill['real_date'])));

            echo CJSON::encode(array('balancelog'=>array($array)));

        }


    }

	public function actionAuth()
	{
		$Account = Account::model()->find("login = :login AND password = :password",
			array(
					":login"=>Yii::app()->params->login,
					":password"=>Yii::app()->params->password)
				);

		if(isset($Account)) 
			echo CJSON::encode(array("auth"=>true));
		else
			echo CJSON::encode(array("auth"=>false));
	}
}


?>